package ch.ethz.smseth;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;

public class MultilineLayout extends ViewGroup
{
	public MultilineLayout(Context context)
	{
		super(context);
		
	}

	public MultilineLayout(Context context, AttributeSet attrs)
	{
		super(context, attrs);
	}
	
	public MultilineLayout(Context context, AttributeSet attrs, int defStyle)
	{
		super(context, attrs, defStyle);
	}
	
	@Override
	public LayoutParams generateLayoutParams(AttributeSet attrs)
	{
		return new MarginLayoutParams(getContext(), attrs);
	}
	
	@Override
	protected void onLayout(boolean changed, int left, int top, int right, int bottom)
	{
		int cnt = getChildCount();

		// first find out how tall and wide each child wants to be,
		// without constrainting the height

		int width = right - left;
		int currX = 0;
		int currY = 0;
		int rowHeight = 0;
		
		for (int i = 0; i != cnt; ++i)
		{
			View c = getChildAt(i);

			if (c == null || c.getVisibility() == GONE)
				continue;

			MarginLayoutParams lp = (MarginLayoutParams) c.getLayoutParams();
			
			if (currX + c.getMeasuredWidth() + lp.leftMargin + lp.rightMargin > width && rowHeight != 0)
			{
				currX = 0;
				currY += rowHeight;
				rowHeight = 0;
			}
			
			rowHeight = Math.max(rowHeight, c.getMeasuredHeight() + lp.topMargin + lp.bottomMargin);
			c.layout(currX + lp.leftMargin, currY + lp.leftMargin, currX + c.getMeasuredWidth() + lp.leftMargin + lp.rightMargin, currY + c.getMeasuredHeight() + lp.topMargin + lp.bottomMargin);
			currX += c.getMeasuredWidth() + lp.leftMargin + lp.rightMargin;
		}
	}

	@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
	{
		int cnt = getChildCount();

		// first find out how tall and wide each child wants to be,
		// without constrainting the height

		for (int i = 0; i != cnt; ++i)
		{
			View c = getChildAt(i);

			if (c == null || c.getVisibility() == GONE)
				continue;

			measureChildWithMargins(c, widthMeasureSpec, 0, heightMeasureSpec, 0);
		}

		int width = MeasureSpec.getSize(widthMeasureSpec);
		
		// calculate total measured height of children, and maximum width
		int totalW = 0;
		int totalH = 0;
		int maxH = 0;
		int maxW = 0;

		for (int i = 0; i != cnt; ++i)
		{
			View c = getChildAt(i);
			if (c == null || c.getVisibility() == GONE)
				continue;
			
			MarginLayoutParams lp = (MarginLayoutParams) c.getLayoutParams();
			
			if (totalW + c.getMeasuredWidth() + lp.leftMargin + lp.rightMargin > width && totalW > 0)
			{
				totalH += maxH;
				maxH = 0;
				maxW = Math.max(maxW, totalW);
				totalW = 0;
			}
			
			totalW += c.getMeasuredWidth()+ lp.leftMargin + lp.rightMargin;
			maxH = Math.max(maxH, c.getMeasuredHeight() + lp.topMargin + lp.bottomMargin);
		}

		totalH += maxH;
		maxW = Math.max(maxW, totalW);
		
		// that's it
		setMeasuredDimension(maxW, totalH);
	}
}
